package learn2;
//迭代器的学习
import learn1.Student;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;


public class test {
    public static void main(String[] args) {
        Collection<Student> c = new ArrayList<Student>();
        Collection<Integer> c2 = new ArrayList<Integer>();
        c2.add(1);
        c2.add(2);
        c2.add(3);

        c.add(new Student("张三", 18));
        c.add(new Student("李四", 19));
        c.add(new Student("王五", 20));

        //获取迭代器
        Iterator<Student> it = c.iterator();

        //使用迭代器遍历集合
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s);
            if (s.age == 19) {
                it.remove();
            }
        }
        Student s = it.next();
        System.out.println(s);


//        //使用迭代器删除元素
//        while (it.hasNext()) {
//            Student s = it.next();
//            if (s.age == 19) {
//                it.remove();
//            }
//        }
//
//        //使用迭代器遍历集合
//        for (Iterator<Student> it2 = c.iterator(); it2.hasNext();) {
//            Student s = it2.next();
//            System.out.println(s);
//        }


    }
}
